package com.rc.saas.tenant.common.utils;

import org.springframework.util.CollectionUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author frog
 * @date 2019/12/6
 */
public class TimeUtil {

    public static Boolean isOverlappTime(List<String> list) throws ParseException {

        Collections.sort(list);//排序ASC

        boolean flag = false;//是否重叠标识
        for (int i = 0; i < list.size(); i++) {
            if (i > 0) {
                //跳过第一个时间段不做判断
                String[] itime = list.get(i).split("-");
                for (int j = 0; j < list.size(); j++) {
                    //如果当前遍历的i开始时间小于j中某个时间段的结束时间那么则有重叠，反之没有重叠
                    //这里比较时需要排除i本身以及i之后的时间段，因为已经排序了所以只比较自己之前(不包括自己)的时间段
                    if (j == i || j > i) {
                        continue;
                    }

                    String[] jtime = list.get(j).split("-");
                    //此处DateUtils.compare为日期比较(返回负数date1小、返回0两数相等、返回正整数date1大)
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    int compare = compare(
                            format.parse("2019-11-28" + " " + itime[0] + ":00"),
                            format.parse("2019-11-28" + " " + jtime[1] + ":00")
                    );


                    if (compare <= 0) {
                        flag = true;
                        break;//只要存在一个重叠则可退出内循环
                    }
                }
            }

            //当标识已经认为重叠了则可退出外循环
            if (flag) {
                break;
            }
        }

        return flag;
    }

    public static int compare(Date date1, Date date2) {
        return date1.compareTo(date2);
    }

    public static Boolean isOverlappDate(List<String> list, String beginDateStr, String endDateStr) throws ParseException {
        boolean flag = false;//是否重叠标识
        if(CollectionUtils.isEmpty(list) || StringUtil.isBlank(beginDateStr) || StringUtil.isBlank(endDateStr)){
            return flag;
        }

        Collections.sort(list);//排序ASC
        for (String dateList : list) {
            String[] itime = dateList.split(",");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date1 = sdf.parse(itime[0]);
            Date date2 = sdf.parse(itime[1]);
            Date beginDate = sdf.parse(beginDateStr);
            Date endDate = sdf.parse(endDateStr);
            if(date1.getTime() <= beginDate.getTime() && date2.getTime() >= beginDate.getTime()){
                flag = true;
                return flag;
            }
            if(date1.getTime() <= endDate.getTime() && date2.getTime() >= endDate.getTime()){
                flag = true;
                return flag;
            }
        }
        return flag;
    }

    public static void main(String[] args) throws ParseException {
        List<String> list = new ArrayList<>();
        list.add("2019-11-28"+ "," + "2019-12-28");
        list.add("2022-11-28"+ "," + "2022-12-28");
        System.out.println(isOverlappDate(list, "2019-11-28", "2019-12-30"));
        System.out.println(isOverlappDate(list, "2018-11-28", "2019-12-30"));
        System.out.println(isOverlappDate(list, "2022-11-29", "2022-12-30"));
    }

}
